package com.qdxwx.web.controller;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.advert.AdvertService;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.picture.PictureAlbumService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookQuery;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.picture.PictureAlbum;
import com.qdxwx.models.picture.PictureAlbumVo;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.teacher.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController extends BaseController {
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private AdvertService advertService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private PictureAlbumService pictureAlbumService;
	@Autowired
	private BookService bookService;

	/**
	 * 首页获取网站首页数据
	 */
	@RequestMapping({"/", "/index.html"})
	public String showIndexPage(HttpServletRequest request, ModelMap model) {
		String cacheKey;
		/*
		//查询广告图数据
		cacheKey = CacheConstants.INDEX_PREFIX + "banner:0";
		List<Advert> advertList = (List<Advert>) CacheUtils.get(cacheKey);
		if (advertList == null || advertList.size() < 1) {
			Advert imager = new Advert();
			imager.setTypeId(1);
			imager.setChannelId(1);
			advertList = advertService.getList(imager, new PageRequest(1,3));
			if (advertList != null && advertList.size() > 0) {
				CacheUtils.set(cacheKey, advertList, CacheConstants.BANNER_IMAGES_TIME);
			}
		}
		model.put("advertList", advertList);
		*/
		/*
		//近期直播章节
		cacheKey = CacheConstants.INDEX_PREFIX+"course:lives";
		List<CourseNodeDTO> liveCourseList = (List<CourseNodeDTO>) CacheUtils.get(cacheKey);
		if(liveCourseList==null || liveCourseList.size() < 1) {
			CourseNode courseNode = new CourseNode();
			courseNode.setFileType("LIVE");
			courseNode.setNodeType(1);
			courseNode.setQueryOrder("near");
			courseNode.setQueryLimitNum(4);
			courseNode.setIsAvailable(1);
			//结束时间大于今天的  正在播放 和未开始的
			courseNode.setLiveEndTime(LocalDateTime.now());
			//去重查询直播最近开始的直播章节
			liveCourseList = courseNodeService.queryCourseNearestNodeList(courseNode);
			//如果没有要开始的直播章节就查询最近结束的4个
			if (ObjectUtils.isNull(liveCourseList)) {
				courseNode.setLiveEndTime(null);
				courseNode.setQueryOrder("overNear");//近期结束章节
				liveCourseList = courseNodeService.queryCourseNearestNodeList(courseNode);
			}
			if (liveCourseList != null && liveCourseList.size() > 0) {
				CacheUtils.set(cacheKey, liveCourseList, 3600);
			}
		}
		model.put("liveCourseList", liveCourseList);
		*/

		//同步课
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setChannelId(2);
		courseQuery.setIsAvailable(1);
		courseQuery.setSellType(CourseType.COURSE.toString());
		cacheKey = CacheConstants.INDEX_PREFIX + "course:news";
		List<Course> newCourseList = (List<Course>) CacheUtils.get(cacheKey);
		if (newCourseList == null || newCourseList.size() < 1) {
			newCourseList = courseService.getList(courseQuery, new PageRequest(1, 6));
			if (newCourseList != null && newCourseList.size() > 0) {
				CacheUtils.set(cacheKey, newCourseList, 1800);
			}
		}
		courseQuery.setIsElite(1);
		cacheKey = CacheConstants.INDEX_PREFIX + "course:elites";
		List<Course> eliteCourseList = (List<Course>) CacheUtils.get(cacheKey);
		if (eliteCourseList == null || eliteCourseList.size() < 1) {
			eliteCourseList = courseService.getList(courseQuery, new PageRequest(1, 6));
			if (eliteCourseList != null && eliteCourseList.size() > 0) {
				CacheUtils.set(cacheKey, eliteCourseList, 3600);
			}
		}
		model.put("newCourseList", newCourseList);
		model.put("eliteCourseList", eliteCourseList);
		//大师课
		courseQuery.setChannelId(8);
		cacheKey = CacheConstants.INDEX_PREFIX + "course:pro:elites";
		List<Course> eliteProCourseList = (List<Course>) CacheUtils.get(cacheKey);
		if (eliteProCourseList == null || eliteProCourseList.size() < 1) {
			eliteProCourseList = courseService.getList(courseQuery, new PageRequest(1, 6));
			if (eliteProCourseList != null && eliteProCourseList.size() > 0) {
				CacheUtils.set(cacheKey, eliteProCourseList, 3600);
			}
		}
		courseQuery.setIsElite(null);
		cacheKey = CacheConstants.INDEX_PREFIX + "course:pro:news";
		List<Course> newCourseListPro = (List<Course>) CacheUtils.get(cacheKey);
		if (newCourseListPro == null || newCourseListPro.size() < 1) {
			newCourseListPro = courseService.getList(courseQuery, new PageRequest(1, 6));
			if (newCourseListPro != null && newCourseListPro.size() > 0) {
				CacheUtils.set(cacheKey, newCourseListPro, 1800);
			}
		}
		model.put("newCourseListPro", newCourseListPro);
		model.put("eliteCourseListPro", eliteProCourseList);

		//素材
		cacheKey = CacheConstants.INDEX_PREFIX + "picture:albums";
		List<PictureAlbumVo> pictureAlbumVoList = (List<PictureAlbumVo>) CacheUtils.get(cacheKey);
		if (pictureAlbumVoList == null || pictureAlbumVoList.size() < 1) {
			PictureAlbum pictureAlbum = new PictureAlbum();
			pictureAlbum.setIsElite(1);
			pictureAlbum.setIsTop(1);
			pictureAlbumVoList = pictureAlbumService.getListVo(pictureAlbum, new PageRequest(1, 8));
			CacheUtils.set(cacheKey, pictureAlbumVoList, 3600);
		}
		model.put("pictureAlbumVoList", pictureAlbumVoList);

		cacheKey = CacheConstants.INDEX_PREFIX + "teacher:news";
		List<Teacher> teacherList = (List<Teacher>) CacheUtils.get(cacheKey);
		if (teacherList == null || teacherList.size() < 1) {
			TeacherQuery teacher = new TeacherQuery();
			teacher.setIsTop(1);
			teacher.setIsDisabled(0);
			teacherList = teacherService.getList(teacher, new PageRequest(1, 8));
			CacheUtils.set(cacheKey, teacherList, 3600);
		}
		model.put("teacherList", teacherList);

		cacheKey = CacheConstants.INDEX_PREFIX + "book:tops";
		List<Book> bookLists = (List<Book>) CacheUtils.get(cacheKey);
		if (bookLists == null || bookLists.size() < 1) {
			BookQuery book = new BookQuery();
			book.setIsElite(1);
			book.setIsTop(1);
			bookLists = bookService.getList(book, new PageRequest(1, 6));
			CacheUtils.set(cacheKey, bookLists, 1800);
		}
		model.put("bookLists", bookLists);

		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("index");
		} else {
			return getViewPath("index");
		}
	}

	@RequestMapping("/share.html")
	public String share(HttpServletRequest request, ModelMap mmap) {
		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("share");
		} else {
			return getViewPath("share");
		}
	}
}